package cn.powerone.scm.service.impl;

import java.util.List;

import cn.powerone.common.exception.ServiceException;
import cn.powerone.common.utils.DateUtils;
import cn.powerone.common.utils.StringUtils;
import cn.powerone.scm.domain.ScmSupplier;
import cn.powerone.scm.mapper.ScmSupplierMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.powerone.scm.mapper.ScmSupplierAddressMapper;
import cn.powerone.scm.domain.ScmSupplierAddress;
import cn.powerone.scm.service.IScmSupplierAddressService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 供应商地址Service业务层处理
 * 
 * @author PowerOne
 * @date 2025-06-26
 */
@Service
public class ScmSupplierAddressServiceImpl implements IScmSupplierAddressService 
{
    @Autowired
    private ScmSupplierAddressMapper scmSupplierAddressMapper;

    @Autowired
    private ScmSupplierMapper scmSupplierMapper;
    /**
     * 查询供应商地址
     * 
     * @param id 供应商地址主键
     * @return 供应商地址
     */
    @Override
    public ScmSupplierAddress selectScmSupplierAddressById(Long id)
    {
        return scmSupplierAddressMapper.selectScmSupplierAddressById(id);
    }

    /**
     * 查询供应商地址列表
     * 
     * @param scmSupplierAddress 供应商地址
     * @return 供应商地址
     */
    @Override
    public List<ScmSupplierAddress> selectScmSupplierAddressList(ScmSupplierAddress scmSupplierAddress)
    {
        return scmSupplierAddressMapper.selectScmSupplierAddressList(scmSupplierAddress);
    }

    /**
     * 新增供应商地址
     * 
     * @param scmSupplierAddress 供应商地址
     * @return 结果
     */
    @Override
    public int insertScmSupplierAddress(ScmSupplierAddress scmSupplierAddress)
    {
        scmSupplierAddress.setCreateTime(DateUtils.getNowDate());
        return scmSupplierAddressMapper.insertScmSupplierAddress(scmSupplierAddress);
    }

    /**
     * 修改供应商地址
     * 
     * @param scmSupplierAddress 供应商地址
     * @return 结果
     */
    @Override
    public int updateScmSupplierAddress(ScmSupplierAddress scmSupplierAddress)
    {
        scmSupplierAddress.setUpdateTime(DateUtils.getNowDate());
        return scmSupplierAddressMapper.updateScmSupplierAddress(scmSupplierAddress);
    }

    /**
     * 批量删除供应商地址
     * 
     * @param ids 需要删除的供应商地址主键
     * @return 结果
     */
    @Override
    public int deleteScmSupplierAddressByIds(Long[] ids)
    {
        return scmSupplierAddressMapper.deleteScmSupplierAddressByIds(ids);
    }

    /**
     * 删除供应商地址信息
     * 
     * @param id 供应商地址主键
     * @return 结果
     */
    @Override
    public int deleteScmSupplierAddressById(Long id)
    {
        return scmSupplierAddressMapper.deleteScmSupplierAddressById(id);
    }

    /** 导入数据
     * @param scmSupplierAddresses 导入询价Batch列表
     */
    @Transactional
    @Override
    public String importData(List<ScmSupplierAddress> scmSupplierAddresses, boolean updateSupport, String operName)
    {
        if (StringUtils.isNull(scmSupplierAddresses) || scmSupplierAddresses.isEmpty())
        {
            throw new RuntimeException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        ScmSupplier customerInq = new ScmSupplier();
        ScmSupplierAddress scmSupplierAddressInq = new ScmSupplierAddress();
        long numberOfResult = 0;
        List<ScmSupplier> customerOutList;
        for (ScmSupplierAddress item : scmSupplierAddresses)
        {
            if(item.getSupplierNo() != null && !item.getSupplierNo().isEmpty()){
                customerInq.setSupplierNo(item.getSupplierNo());
                customerOutList = scmSupplierMapper.selectScmSupplierList(customerInq);
                if(customerOutList.isEmpty()){
                    continue;
                }
                item.setSupplierId(customerOutList.get(0).getId());
            }
            if (!updateSupport){
                scmSupplierAddressInq.setSupplierId(item.getSupplierId());
                scmSupplierAddressInq.setDetailAddress(item.getDetailAddress());
                numberOfResult = scmSupplierAddressMapper.selectScmSupplierAddressList(scmSupplierAddressInq).size();
                if (numberOfResult > 0 ){
                    continue;
                }
            }
            try
            {
                scmSupplierAddressMapper.insertScmSupplierAddress( item);
                successNum++;
            } catch (Exception e)
            {
                failureNum++;
            }
        }
        if (failureNum > 0){
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条地址数据格式不正确");
            throw new ServiceException(failureMsg.toString());
        }else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条。");
        }
        return successMsg.toString();

    }
}
